package com.snoweagler.JmsSend;

import com.snoweagler.VO.PageVO;
import com.snoweagler.VO.ResponseVO;
import com.snoweagler.params.RoleParams;
import com.snoweagler.pojo.Menu;
import com.snoweagler.pojo.Role;
import com.snoweagler.service.RoleService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;

    @RequestMapping("/list")
    public PageVO<Role> getUserList(RoleParams params) {
        return roleService.getUserList(params);
    }

    @RequestMapping("/edit")
    public ResponseVO edit(Role role) {
        try {
            roleService.edit(role);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseVO.error();
        }
        return ResponseVO.ok();
    }

    @RequestMapping("/treeData")
    public Object treeData(long pid) {
        List<Menu> mapList = roleService.treeDataSelect(pid);
        for (Menu menu : mapList) {
            Long id = menu.getId();
            menu.setChildren(new ArrayList<Menu>());
            List<Menu> menus = roleService.treeDataSelect(id);
            for (Menu menu1 : menus) {
                menu.getChildren().add(menu1);
                Long id1 = menu1.getId();
                if (id1 != null) {
                    menu1.setChildren(new ArrayList<Menu>());
                    List<Menu> ml = roleService.treeDataSelect(id1);
                    for (Menu menu2 : ml) {
                        menu1.getChildren().add(menu2);
                    }
                }
            }
        }
        return mapList;
    }

    @RequestMapping("/menuids/{id}")
    public List menuids(@PathVariable("id") Long id) {
        List<Long> longList = roleService.getmenuids(id);
        return longList;
    }
}
